﻿/*
 * base on bootstrap3.0 style
 * 
 */
(function () {
    'use strict';
    appModule.directive('navPage', function () {
        return {
            restrict: 'E',
            replace: true,
            template: '<nav>' +
                        '<ul class="pagination">' +
                            '<li ng-class="{disabled:currentPage==1}">' +
                                '<a href="javascrip:void" ng-click="Previous($event)" aria-label="Previous">' +
                                    '<span class="fa fa-caret-left fa-lg"></span>' +
                                '</a>' +
                            '</li>' +
                            '<li ng-show="PreviousBar"  ng-class=""><a ng-click="PreviousPage()" href="#">...</a></li>' +
                            '<li ng-repeat="n in pageBar" ng-class="{active: n == currentPage}"><a ng-click="goPage(n)" href="#">{{n}}</a></li>' +
                            '<li ng-show="NextPageBar"  ng-class=""><a ng-click="NextPage()" href="#">...</a></li>' +
                            '<li ng-class="{disabled:currentPage==totalPage}" >' +
                                '<a href="javascrip:void" ng-click="Next($event)" aria-label="Next">' +
                                    '<span class="fa fa-caret-right fa-lg"></span>' +
                                '</a>' +
                            '</li>' +
                        '</ul>' +
                      '</nav>',
            scope: {
                pageSize: '=',
                totalCount: '=',
                query: '='

            },
            link: function (scope, element, attr) {

                scope.currentPage = 1;
                scope.pageSize = !scope.pageSize ? 10 : scope.pageSize;                
                scope.totalCount = !scope.totalCount ? 1 : scope.totalCount;
                scope.totalPage = scope.totalCount < scope.pageSize ? 1 : ((scope.totalCount / scope.pageSize) + (scope.totalCount % scope.pageSize == 0 ? 0 : 1));
                scope.pageBarCount = 10;

                scope.PreviousBar = (scope.currentPage > scope.pageBarCount);
                scope.NextPageBar = Math.ceil(scope.currentPage / scope.pageBarCount) < Math.ceil(scope.totalPage / scope.pageBarCount);

                scope.pageBar = [];
                var getTotalPages = function () {

                    var pageBarLength = scope.pageBar.length = 0;
                    var beginPage = 1;
                    scope.pageBar.length = 0;

                    beginPage = parseInt(scope.currentPage / 10) + 1;
                    beginPage = (beginPage != 1 && (parseInt(scope.currentPage / 10) != (scope.currentPage / 10)))
                        ? parseInt((beginPage - 1) + '1') :
                            ((scope.currentPage / 10) > 1 ? parseInt((beginPage - 2) + '1') : 1);

                    scope.NextPageBar = Math.ceil(scope.currentPage / scope.pageBarCount) < Math.ceil(scope.totalPage / scope.pageBarCount);
                    scope.PreviousBar = (scope.currentPage > scope.pageBarCount);

                    for (var i = beginPage; i < (scope.pageBarCount + beginPage) && i <= scope.totalPage ; i++) {
                        scope.pageBar.push(i);
                    }

                    if (scope.query instanceof Function) scope.query(scope.pageSize, scope.currentPage);
                };
                scope.renderpage = function () {
                    scope.currentPage++;
                    getTotalPages();
                };
                getTotalPages();

                scope.PreviousPage = function () {

                    var curPage = scope.currentPage;
                    var pageBarCount = scope.pageBarCount;
                    scope.currentPage = (curPage % 10) == 0 ? (curPage - pageBarCount) : curPage - (curPage % 10);
                    getTotalPages();
                };
                scope.NextPage = function () {

                    var curPage = scope.currentPage;
                    var pageBarCount = scope.pageBarCount;
                    scope.currentPage = (curPage % 10) == 0 ? (curPage + 1) : curPage + (pageBarCount - (curPage % 10) + 1);
                    getTotalPages();
                };

                //上一页
                scope.Previous = function (e) {
                    var $Previous = $(e.target).parents('li');
                    if ($Previous.hasClass('disabled')) {
                        return;
                    }
                    if (scope.currentPage > 1) scope.currentPage--;
                    getTotalPages();
                };
                //下一页
                scope.Next = function (e) {
                    var $Next = $(e.target).parents('li');
                    if ($Next.hasClass('disabled')) {
                        return;
                    }
                    if (scope.currentPage < scope.totalPage) scope.currentPage++;

                    getTotalPages();
                };
                scope.goPage = function (n) {
                    if (scope.currentPage != n) scope.currentPage = n;
                    if (scope.query instanceof Function) scope.query(scope.pageSize, scope.currentPage);
                };
            }
        };
    });
})();